package cli;

/**
 * @ClassName DeadLock
 * @Author ykx
 * @Date 2024/6/30 18:06
 * @Version 1.0
 */
public class DeadLock {

    private static Object obj1 = new Object();

    private static Object obj2 = new Object();

    public static void main(String[] args) {

        new Thread(new Thread1()).start();

        new Thread(new Thread2()).start();

    }

    private static class Thread1 implements Runnable {

        public void run() {

            synchronized (obj1) {

                System.out.println("Thread1 拿到了 obj1 ጱᲁ的锁");

                try {

                    Thread.sleep(2000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized (obj2) {

                    System.out.println("Thread1 拿到了 obj2 ጱᲁ的锁");

                }

            }

        }

    }

    private static class Thread2 implements Runnable {

        public void run() {

            synchronized (obj2) {

                System.out.println("Thread2 拿到了 obj2 的锁");

                try {


                    Thread.sleep(2000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                synchronized (obj1) {

                    System.out.println("Thread2 拿到了 obj1 的锁");

                }

            }

        }

    }
}
